<?php

namespace app\common\service\setting;
use app\common\cache\setting\SettingStorePlatformCache;
use app\common\model\setting\SettingStorePlatformModel;

/**
 * 店铺平台
 */
class SettingStorePlatformService
{
    /**
     * 添加、修改字段
     * @var array
     */
    public static $edit_field = [
        'id' => '',
        'title' => '',
        'code' => '',
        'is_disable' => '',
        'image_id/d' => null,
        'service_money/f' => 0,
        'sort/d' =>250,
        'remark' => '',
        'account_number/d' => 1,//买手账号数量
        'evaluate_image_id/d' => null,//订单评价示意图
        'last_month_image_id/d' => null,//上月订单示意图
        'month_image_id/d' => null,//上上月订单示意图
        'personal_center_image_id/d' => null,//个人中心示意图
    ];
    /**
     * 店铺平台列表
     *
     * @param array  $where 条件
     * @param int    $page  页数
     * @param int    $limit 数量
     * @param array  $order 排序
     * @param string $field 字段
     *
     * @return array
     */
    public static function list($where = [], $page = 1, $limit = 10,  $order = [], $field = '')
    {
        $model = new SettingStorePlatformModel();
        $pk = $model->getPk();
        if (empty($field)) {
            $field = 'id,title,code,is_disable,create_time,update_time,image_id,service_money,sort,remark,account_number';
        }
        if (empty($order)) {
            $order = ['sort'=>'asc',$pk => 'desc'];
        }
        $with = $append = $hidden = [];
        if (strpos($field, 'image_id') !== false) {
            $with[]   = $hidden[] = 'image';
            $append[] = 'image_url';
        }
        if (strpos($field, 'evaluate_image_id') !== false) {
            $with[]   = $hidden[] = 'evaluateImage';
            $append[] = 'evaluate_image_url';
        }
        if (strpos($field, 'last_month_image_id') !== false) {
            $with[]   = $hidden[] = 'lastMonthImage';
            $append[] = 'last_month_image_url';
        }
        if (strpos($field, 'month_image_id') !== false) {
            $with[]   = $hidden[] = 'monthImage';
            $append[] = 'month_image_url';
        }
        if (strpos($field, 'personal_center_image_id') !== false) {
            $with[]   = $hidden[] = 'personalCenterImage';
            $append[] = 'personal_center_image_url';
        }
        if ($page == 0 || $limit == 0) {
            return $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->order($order)->select()->toArray();
        }
        $count = $model->where($where)->count();
        $pages = ceil($count / $limit);
        $list = $model->with($with)->append($append)->hidden($hidden)->field($field)->where($where)->page($page)->limit($limit)->order($order)->select()->toArray();
        return compact('count', 'pages', 'page', 'limit', 'list');
    }
    /**
     * 店铺平台信息
     *
     * @param int  $id   店铺平台id
     * @param bool $exce 不存在是否抛出异常
     *
     * @return array|Exception
     */
    public static function info($id, $exce = true, $field = '')
    {
        $key = where_cache_key('info', [['id','=',$id]], [], $field);
        $info = SettingStorePlatformCache::get($key);
        if (empty($info)) {
            if (empty($field)) {
                $field = 'id,title,code,is_disable,is_delete,create_uid,update_uid,delete_uid,create_time,update_time,delete_time,image_id,service_money,sort,remark,account_number,evaluate_image_id,last_month_image_id,month_image_id,personal_center_image_id';
            }
            $with = $append = $hidden = [];
            if (strpos($field, 'image_id') !== false) {
                $with[]   = $hidden[] = 'image';
                $append[] = 'image_url';
            }
            if (strpos($field, 'evaluate_image_id') !== false) {
                $with[]   = $hidden[] = 'evaluateImage';
                $append[] = 'evaluate_image_url';
            }
            if (strpos($field, 'last_month_image_id') !== false) {
                $with[]   = $hidden[] = 'lastMonthImage';
                $append[] = 'last_month_image_url';
            }
            if (strpos($field, 'month_image_id') !== false) {
                $with[]   = $hidden[] = 'monthImage';
                $append[] = 'month_image_url';
            }
            if (strpos($field, 'personal_center_image_id') !== false) {
                $with[]   = $hidden[] = 'personalCenterImage';
                $append[] = 'personal_center_image_url';
            }
            $model = new SettingStorePlatformModel();
            $info = $model->with($with)->append($append)->hidden($hidden)->field($field)->find($id);
            if (empty($info)) {
                if ($exce) {
                    exception('店铺平台不存在：' . $id);
                }
                return [];
            }
            $info = $info->toArray();
            SettingStorePlatformCache::set($key, $info);
        }
        return $info;
    }
    /**
     * 店铺平台添加
     *
     * @param array $param 店铺平台信息
     *
     * @return array|Exception
     */
    public static function add($param)
    {
        $model = new SettingStorePlatformModel();
        $pk = $model->getPk();
        unset($param[$pk]);
        $param['create_uid']  = user_id();
        $param['create_time'] = datetime();
        $model->save($param);
        $id = $model->$pk;
        if (empty($id)) {
            exception();
        }
        $param[$pk] = $id;
        return $param;
    }
     /**
     * 店铺平台修改
     *
     * @param int|array $ids   店铺平台id
     * @param array     $param 店铺平台信息
     *
     * @return array|Exception
     */
    public static function edit($ids, $param = [])
    {
        $model = new SettingStorePlatformModel();
        $pk = $model->getPk();
        unset($param[$pk], $param['ids']);
        $param['update_uid']  = user_id();
        $param['update_time'] = datetime();
        $res = $model->where($pk, 'in', $ids)->update($param);
        if (empty($res)) {
            exception();
        }
        $param['ids'] = $ids;
//        SettingStorePlatformCache::del($ids);
        SettingStorePlatformCache::clear();
        return $param;
    }
    /**
     * 店铺平台删除
     *
     * @param array $ids  店铺平台id
     * @param bool  $real 是否真实删除
     *
     * @return array|Exception
     */
    public static function dele($ids, $real = false)
    {
        $model = new SettingStorePlatformModel();
        $pk = $model->getPk();
        if ($real) {
            $res = $model->where($pk, 'in', $ids)->delete();
        } else {
            $update = delete_update();
            $res = $model->where($pk, 'in', $ids)->update($update);
        }
        if (empty($res)) {
            exception();
        }
        $update['ids'] = $ids;
//        SettingStorePlatformCache::del($ids);
        SettingStorePlatformCache::clear();
        return $update;
    }
}
